<!-- 消息框 -->
<template>
  <div class="info-box">
    <div class="info-header_box">
      <chat-header @top-view="$emit('top-view')"></chat-header>
    </div>
    <div class="info-content_box" ref="wrap" @scroll="_handleScroll">
      <div
        class="chat-messages__load"
        :class="{ 'chat-messages__load--': loadend === 'ing' }"
      >
        <!-- <span v-if="loaded" class="chat-messages__loadend"> 暂无更多消息 </span> -->
        <span v-if="!loaded" class="chat-messages__loading">
          <i class="el-icon-loading" />
        </span>
      </div>

      <div v-for="(item, index) in messageInfo.messages" :key="item.id">
        <div
          class="chat-message-event"
          v-if="item.type === MESSAGE_TYPE.NOTICE"
        >
          <span class="chat-message-event__content"> {{ item.content }}</span>
        </div>
        <div
          class="chat-message-event"
          v-else-if="item.type === MESSAGE_TYPE.RECALL"
        >
          <span class="chat-message-event__content">
            {{ item.content.replace(`"${userInfo.userName}" `, `你`)
            }}<span
              v-if="
                item.recallText &&
                userInfo.userId == item.senderId &&
                item.sendTime > new Date().getTime() - 2 * 60 * 1000
              "
              @click="handleResetEdit(item.recallText)"
              class="chat-message_reset_edit"
            >
              重新编辑</span
            ></span
          >
        </div>
        <template v-else>
          <div
            class="chat-message-text"
            :class="
              'chat-message-text_' +
              (item.senderId == currentId ? 'right' : 'left')
            "
          >
            <div class="message_inner">
              <div class="top">
                <div class="time">
                  {{ timeFormat(item.sendTime) }}
                </div>
                <span class="name">{{ item.senderName }}</span>
              </div>
              <div class="bottom">
                <div v-if="item.type === MESSAGE_TYPE.FILE">
                  <file-content
                    v-contextmenu="{
                      data: { ...item, index },
                      operateList: contactContextmenu
                    }"
                    @resetUploadSuccess="handleResetUploadSuccess"
                    :ref="item.id ? item.id : item.uuid"
                    :fileInfo="item"
                  ></file-content>
                </div>
                <template v-else>
                  <i v-if="item.status === 'ing'" class="el-icon-loading" />
                  <div
                    class="msg"
                    v-html="item.content"
                    v-contextmenu="{
                      data: { ...item, index },
                      operateList: contactContextmenu
                    }"
                  ></div>
                </template>
              </div>
            </div>
            <el-popover
              placement="right"
              width="387"
              height="359"
              popper-class="avatar-info_popover"
              @show="handleShowAvatarInfo(item)"
              trigger="click"
            >
              <div class="avatar_info">
                <div class="top">
                  <chat-avatar
                    class="avatar"
                    :size="68"
                    :content="
                      item.senderAvatar ? item.senderAvatar : item.senderName
                    "
                  />
                  <div class="text">
                    {{ item.senderName }}（{{ clickUserInfo.code }}）
                  </div>
                </div>
                <div class="bottom">
                  <ul>
                    <li>
                      <div class="label">职位：</div>
                      <div class="value">{{ clickUserInfo.duties }}</div>
                    </li>
                    <li>
                      <div class="label">部门：</div>
                      <div class="value">
                        {{ clickUserInfo.departmentName }}
                      </div>
                    </li>
                    <li>
                      <div class="label">值班电话：</div>
                      <div class="value">{{ clickUserInfo.onDutyPhone }}</div>
                    </li>
                    <li>
                      <div class="label">工作电话</div>
                      <div class="value">{{ clickUserInfo.workPhone }}</div>
                    </li>
                    <li>
                      <div class="label">手机号：</div>
                      <div class="value">{{ clickUserInfo.phone }}</div>
                    </li>
                    <li>
                      <div class="label">个性签名：</div>
                      <div class="value">{{ clickUserInfo.motto }}</div>
                    </li>
                  </ul>
                </div>
              </div>
              <template #reference>
                <chat-avatar
                  style="cursor: pointer"
                  :size="32"
                  :content="
                    item.senderAvatar ? item.senderAvatar : item.senderName
                  "
                />
              </template>
            </el-popover>
          </div>
        </template>
      </div>
    </div>
    <!-- <forward-dialog ref="forward"></forward-dialog> -->
  </div>
</template>
<script>
import chatAvatar from "@/components/common/avatar.vue"
import lockr, { set } from "lockr"
import { copyText, timeFormat } from "@/utils/chat"
import chatHeader from "./header.vue"
import FileContent from "@/components/fileMessage/FileContent.vue"
import { MESSAGE_TYPE } from "@/utils/constant"
import { mapState } from "vuex"
import { messagesRecall } from "@/api/chat"
import { getCurrentChatInfo } from "@/api/user"
import { now } from "lodash"
export default {
  name: "infoPage",
  components: {
    chatAvatar,
    chatHeader,
    FileContent
  },
  props: {
    messageInfo: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },

  data() {
    return {
      accept: "",
      loadend: "end",
      loadendText: "加载",
      currentId: lockr.get("userInfo").userId,
      timeFormat,
      msecRange: "",
      MESSAGE_TYPE,
      resetEditData: null,
      clickUserInfo: {},
      loaded: false
    }
  },

  mounted() {},

  computed: {
    ...mapState({
      activeMenu: (state) => state.activeMenu,
      userInfo: (state) => state.userInfo
    }),
    contactContextmenu() {
      return [
        {
          text: "复制",
          icon: "local-copy-hover",
          click: (e, instance, hide) => {
            copyText(
              instance.contentText,
              () => {
                this.$message.success("复制成功")
              },
              () => {
                this.$message.error("复制失败")
              }
            )

            hide()
          },
          visible: (item) => {
            return item.type !== this.MESSAGE_TYPE.FILE
          }
        },
        {
          text: "下载",
          icon: "local-file-download",
          click: (e, instance, hide) => {
            this.$refs[
              instance.id ? instance.id : instance.uuid
            ][0].handleDownload()
            hide()
          },
          visible: (item) => {
            return item.type === this.MESSAGE_TYPE.FILE
          }
        },
        {
          text: "转发",
          icon: "local-forward",
          click: (e, instance, hide) => {
            this.$forwardDialog.open(instance)
            hide()
          },
          visible: () => {
            return this.$route.path !== "/chat"
          }
        },
        {
          text: "撤回",
          icon: "local-recall",
          click: (e, instance, hide) => {
            if (
              instance.sendTime &&
              instance.sendTime < new Date().getTime() - 120000
            ) {
              this.$message.error("超过两分钟的消息无法撤回")
              return
            }
            this.resetEditData = instance
            this.handleRecall(instance)
            hide()
          },
          visible: (instance) => {
            return (
              instance.sendTime &&
              instance.senderId == this.userInfo.id &&
              instance.sendTime > new Date().getTime() - 120000
            )
          }
        }
      ]
    }
  },

  methods: {
    async _handleScroll(e) {
      const { target } = e
      if (target.scrollTop == 0 && this.loaded == false) {
        await this.$nextTick()
        const hst = target.scrollHeight
        this.$emit("reach-top", async (isEnd) => {
          await this.$nextTick()
          target.scrollTop = target.scrollHeight - hst
        })
      }
    },
    async scrollToBottom() {
      await this.$nextTick()
      const { wrap } = this.$refs
      if (wrap) {
        setTimeout(() => {
          wrap.scrollTop = wrap.scrollHeight
        }, 5)
        setTimeout(() => {
          wrap.scrollTop = wrap.scrollHeight
        }, 100)
      }
    },
    handleResetUploadSuccess(data) {
      this.$emit("resetUploadSuccess", data)
    },
    async handleRecall(instance) {
      this.$emit("recall", instance)
    },
    handleResetEdit(recallText) {
      this.$emit("resetEdit", recallText)
    },
    async handleShowAvatarInfo(item) {
      const res = await getCurrentChatInfo(item.senderId)
      if (res) {
        this.clickUserInfo = res.data
      }
    }
  },

  activeMenu(val) {
    if (val === "badge") {
      this.scrollToBottom()
    }
  }
}
</script>
<style lang="less" scoped>
.info-box {
  display: flex;
  flex-direction: column;
  height: 506px;
  flex: 1;
  width: 100%;
  .info-header_box {
    height: 50px;
  }
  .info-content_box {
    flex: 1;
    border-bottom: 1px solid #c7cdd9;
    border-top: 1px solid #c7cdd9;
    padding: 12px;
    overflow-y: auto;
  }
}

.chat-messages__load {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 14px;
  color: #667e90;
  > span {
    padding: 10px 0;
  }
  .chat-messages__loading i {
    font-size: 18px;
  }
}
.chat-message-event {
  text-align: center;
  padding: 10px 0;
  .chat-message-event__content {
    text-align: center;
    font-size: 14px;
    color: #667e90;
    .chat-message_reset_edit {
      color: #0176f6;
      cursor: pointer;
    }
  }
}
.chat-message-text {
  margin: 32px 0;
}

.message_inner {
  width: 85%;
  padding-left: 12px;
  .top {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #11131a;
    .time {
      font-size: 12px;
      color: #666;
      padding-right: 2px;
    }

    .name {
      padding: 0 2px;
    }
  }
  .bottom {
    margin-top: 8px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    /deep/.chat-set-html {
      .chat-img {
        max-width: 600px;
      }
    }
    .msg {
      background: #ffffff;
      border-radius: 0 7.2px 7.2px 7.2px;
      padding: 10px 13px;
      margin-left: 3px;
    }
  }
}

.chat-message-text_left {
  display: flex;
  flex-direction: row-reverse;
  justify-content: flex-end;
  .message_inner {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    .top {
      flex-direction: row-reverse;
    }
  }
}

.chat-message-text_right {
  display: flex;
  justify-content: flex-end;
  .message_inner {
    padding-right: 12px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    .msg {
      border-radius: 7.2px 0 7.2px 7.2px;
      background: #d7e2ee;
    }
  }
}
</style>
<style lang="less">
.avatar-info_popover {
  width: 100%;
  height: 359px;
  background: url("../../../assets/img/bg.png");
  .avatar_info {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    .top {
      width: 347px;
      height: 106px;
      margin-top: 10px;
      background: rgba(255, 255, 255, 0.5);
      box-shadow: inset 0px 0px 10px 0px rgba(255, 255, 255, 0.5);
      border-radius: 8px;
      .avatar {
        position: relative;
        margin-left: 20px;
        top: -10px;
      }
      .text {
        font-size: 16px;
        color: #000000;
        font-weight: bold;
        margin-left: 20px;
      }
    }
    .bottom {
      width: 347px;
      height: 205px;
      background: rgba(255, 255, 255, 0.5);
      box-shadow: inset 0px 0px 10px 0px rgba(255, 255, 255, 0.5);
      border-radius: 8px;
      ul {
        margin: 14px;
        li {
          display: flex;
          padding-bottom: 13px;
          .label {
            width: 70px;
            color: #999999;
          }

          .value {
            color: #333333;
          }
        }
      }
    }
  }
}
</style>
